package com.showsu.editor.utils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author eollse-wj
 * @ClassName OsAndBrowserUtil
 * @date 2019/12/27
 * @description
 */
public class OsAndBrowserUtil {

    private String os;

    private String browser;

    private String IP;

    public String getOs() {
        return os;
    }

    public void setOs(String os) {
        this.os = os;
    }

    public String getBrowser() {
        return browser;
    }

    public void setBrowser(String browser) {
        this.browser = browser;
    }

    public String getIP() {
        return IP;
    }

    public void setIP(String IP) {
        this.IP = IP;
    }

    private static OsAndBrowserUtil osAndBrowser;

    /**
     *
     * @param request
     * @return
     */
    public static OsAndBrowserUtil getOSAndBrowser(HttpServletRequest request) {
        osAndBrowser = new OsAndBrowserUtil();
        String browserDetails = request.getHeader("User-Agent");
        String userAgent = browserDetails;
        if (!StringUtil.isEmpty(userAgent)) {
            String user = userAgent.toLowerCase();
            if (userAgent.toLowerCase().indexOf("windows") >= 0) {
                osAndBrowser.os = "Windows";
            } else if (userAgent.toLowerCase().indexOf("mac") >= 0) {
                osAndBrowser.os = "Mac";
            } else if (userAgent.toLowerCase().indexOf("x11") >= 0) {
                osAndBrowser.os = "Unix";
            } else if (userAgent.toLowerCase().indexOf("android") >= 0) {
                osAndBrowser.os = "Android";
            } else if (userAgent.toLowerCase().indexOf("iphone") >= 0) {
                osAndBrowser.os = "IPhone";
            } else {
                osAndBrowser.os = "UnKnown, Info: " + userAgent;
            }
            //===============Browser===========================
            if (user.contains("edge")) {
                osAndBrowser.browser = (userAgent.substring(userAgent.indexOf("Edge")).split(" ")[0]).replace("/", "-");
            } else if (user.contains("msie")) {
                String substring = userAgent.substring(userAgent.indexOf("MSIE")).split(";")[0];
                osAndBrowser.browser = substring.split(" ")[0].replace("MSIE", "IE") + "-" + substring.split(" ")[1];
            } else if (user.contains("safari") && user.contains("version")) {
                osAndBrowser.browser = (userAgent.substring(userAgent.indexOf("Safari")).split(" ")[0]).split("/")[0]
                        + "-" + (userAgent.substring(userAgent.indexOf("Version")).split(" ")[0]).split("/")[1];
            } else if (user.contains("opr") || user.contains("opera")) {
                if (user.contains("opera")) {
                    osAndBrowser.browser = (userAgent.substring(userAgent.indexOf("Opera")).split(" ")[0]).split("/")[0]
                            + "-" + (userAgent.substring(userAgent.indexOf("Version")).split(" ")[0]).split("/")[1];
                } else if (user.contains("opr")) {
                    osAndBrowser.browser = ((userAgent.substring(userAgent.indexOf("OPR")).split(" ")[0]).replace("/", "-"))
                            .replace("OPR", "Opera");
                }

            } else if (user.contains("chrome")) {
                osAndBrowser.browser = (userAgent.substring(userAgent.indexOf("Chrome")).split(" ")[0]).replace("/", "-");
            } else if ((user.indexOf("mozilla/7.0") > -1) || (user.indexOf("netscape6") != -1) ||
                    (user.indexOf("mozilla/4.7") != -1) || (user.indexOf("mozilla/4.78") != -1) ||
                    (user.indexOf("mozilla/4.08") != -1) || (user.indexOf("mozilla/3") != -1)) {
                osAndBrowser.browser = "Netscape-?";

            } else if (user.contains("firefox")) {
                osAndBrowser.browser = (userAgent.substring(userAgent.indexOf("Firefox")).split(" ")[0]).replace("/", "-");
            } else if (user.contains("rv")) {
                String IEVersion = (userAgent.substring(userAgent.indexOf("rv")).split(" ")[0]).replace("rv:", "-");
                osAndBrowser.browser = "IE" + IEVersion.substring(0, IEVersion.length() - 1);
            } else {
                osAndBrowser.browser = "UnKnown, Info: " + userAgent;
            }

            //===============IP===========================
            String ip = request.getHeader("x-forwarded-for");
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
            osAndBrowser.IP = ip;
        }
        return osAndBrowser;
    }
}
